//
//  LBDownloaderWork.m
//  LBDownloader
//
//  Created by WuShiHai on 19/6/14.
//  Copyright (c) 2019 Lebbay All rights reserved.
//

#import "LBDownloaderWork.h"
#import "LBDownloaderFolderUtil.h"

#import <SSZipArchive/ZipArchive.h>

@implementation LBDownloaderWork

- (instancetype)init{
    self = [super init];
    if (self){
        self.state = LBDownloaderFileStateUnkown;
    }
    return self;
}

- (LBDownloaderFileState)downloaderFileState{
    if (_state == LBDownloaderFileStateUnkown){
        if ([self isUnziped]){
            _state = LBDownloaderFileStateUnziped;
        }
        else if ([self isDownloaded]){
            _state = LBDownloaderFileStateDownloaded;
        }
        else{
            _state = LBDownloaderFileStateNone;
        }
    }
    return _state;
}

//======== - State
//是否下载完成
- (BOOL)isDownloaded{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL exist = [fileManager fileExistsAtPath:[self.downloader filePath]];
    return exist;
}

//压缩包是否解压完成
- (BOOL)isUnziped{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL exist = [fileManager fileExistsAtPath:[self.downloader unzipStateFilePath]];
    return exist;
}

- (NSString *)identity {
    return [self.downloader identity];
}

- (BOOL)copyFile:(NSString *)filePath
         isUnzip:(BOOL)isUnzip {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath]) {
        return NO;
    }
    LBDownloaderModel *downloaderModel = [[LBDownloaderModel alloc] init];
    downloaderModel.url = filePath;
    downloaderModel.isUnzip = isUnzip;
    self.downloader = downloaderModel;
    
    if ([self isUnziped]) {
        return YES;
    }
    
    if ([LBDownloaderFolderUtil inspectFolderPath:[self.downloader.filePath stringByDeletingLastPathComponent]]) {
        NSError *error = nil;
        self.state = LBDownloaderFileStateDownloading;
        if ([fileManager copyItemAtPath:filePath
                                 toPath:self.downloader.filePath
                                  error:&error]) {
            self.state = LBDownloaderFileStateDownloaded;
            self.state = LBDownloaderFileStateUnziping;
            if ([SSZipArchive unzipFileAtPath:self.downloader.filePath
                                toDestination:self.downloader.unzipFolderPath]){
                self.state = LBDownloaderFileStateUnziped;
                if ([fileManager createFileAtPath:self.downloader.unzipStateFilePath
                                         contents:nil
                                       attributes:nil]) {
#if DEBUG
                    [fileManager createFileAtPath:self.downloader.debugInfoFilePath
                                         contents:[self.downloader.url dataUsingEncoding:NSUTF8StringEncoding]
                                       attributes:nil];
#endif
                    return YES;
                }
            } else {
                self.state = LBDownloaderFileStateUnzipFailed;
            }
        } else {
            self.state = LBDownloaderFileStateDownloadFailed;
        }
    }
    return NO;
}

@end
